/*
 * Copyright 2014 Semidata. All rights reserved.
 */
package com.semidata.trp.report.config;

import java.io.File;

import com.semidata.rtd.core.config.DefaultValue;
import com.semidata.rtd.core.config.DefaultValueClass;
import com.semidata.rtd.core.config.DefaultValueHandler;
import com.semidata.rtd.core.config.Property;

/**
 * @author shewei@semidata.com, She Wei
 * 
 */
public class Config extends com.semidata.rtd.core.config.Config {
	private static String DEFAULT_CONFIG_FILE = "conf/report.conf";
	private static Config config = null;

	public static void init(String configFile) {
		if (configFile == null)
			configFile = Thread.currentThread().getContextClassLoader()
			        .getResource(DEFAULT_CONFIG_FILE).getPath();

		config = new Config(configFile);
	}

	public static Config getConfig() {
		return config;
	}

	private Config(String configFile) {
		initConfig(configFile);
	}

	public static class DefaultDir implements DefaultValueHandler {

		private String path;

		public DefaultDir(String path) {
			this.path = path;
		}

		@Override
		public String value() {
			File file = new File(Thread.currentThread().getContextClassLoader()
			        .getResource(DEFAULT_CONFIG_FILE).getPath());
			file = new File(file.getParentFile().getParentFile(), "data/report");
			file = new File(file, path);
			return file.getAbsolutePath();
		}
	}

	public static class DefaultDirMonitor extends DefaultDir {
		public DefaultDirMonitor() {
			super("monitor");
		}
	}

	public static class DefaultDirMinute extends DefaultDir {
		public DefaultDirMinute() {
			super("minute");
		}
	}

	public static class DefaultDirHour extends DefaultDir {
		public DefaultDirHour() {
			super("hour");
		}
	}

	public static class DefaultDirDay extends DefaultDir {
		public DefaultDirDay() {
			super("day");
		}
	}

	public static class DefaultDirWeek extends DefaultDir {
		public DefaultDirWeek() {
			super("week");
		}
	}

	public static class DefaultDirMonth extends DefaultDir {
		public DefaultDirMonth() {
			super("month");
		}
	}

	public static class DefaultDirTmp extends DefaultDir {
		public DefaultDirTmp() {
			super("tmp");
		}
	}

	public static class DefaultLogDir implements DefaultValueHandler {

		@Override
		public String value() {
			File file = new File(Thread.currentThread().getContextClassLoader()
			        .getResource(DEFAULT_CONFIG_FILE).getPath());
			file = new File(file.getParentFile().getParentFile(), "logs/report");
			return file.getAbsolutePath();
		}

	}

	@Property(value = "log.root")
	@DefaultValueClass(DefaultLogDir.class)
	private String logRoot;

	@Property("log.debug.enabled")
	@DefaultValue("false")
	private boolean logDebugEnabled;

	@Property("report.dir.monitor")
	@DefaultValueClass(DefaultDirMonitor.class)
	private String reportDirMonitor;

	@Property("report.dir.minute")
	@DefaultValueClass(DefaultDirMinute.class)
	private String reportDirMinute;

	@Property("report.dir.hour")
	@DefaultValueClass(DefaultDirHour.class)
	private String reportDirHour;

	@Property("report.dir.day")
	@DefaultValueClass(DefaultDirDay.class)
	private String reportDirDay;

	@Property("report.dir.week")
	@DefaultValueClass(DefaultDirWeek.class)
	private String reportDirWeek;

	@Property("report.dir.month")
	@DefaultValueClass(DefaultDirMonth.class)
	private String reportDirMonth;

	@Property("report.dir.tmp")
	@DefaultValueClass(DefaultDirTmp.class)
	private String reportDirTmp;

	@Property("report.threads")
	@DefaultValue("8")
	private int threads;

	@Property("report.wait")
	@DefaultValue("5000")
	private long waitTime;

	public String getLogRoot() {
		return logRoot;
	}

	public boolean isLogDebugEnabled() {
		return logDebugEnabled;
	}

	public String getReportDirMonitor() {
		return reportDirMonitor;
	}

	public String getReportDirMinute() {
		return reportDirMinute;
	}

	public String getReportDirHour() {
		return reportDirHour;
	}

	public String getReportDirDay() {
		return reportDirDay;
	}

	public String getReportDirWeek() {
		return reportDirWeek;
	}

	public String getReportDirMonth() {
		return reportDirMonth;
	}

	public String getReportDirTmp() {
		return reportDirTmp;
	}

	public int getThreads() {
		return threads;
	}

	public long getWaitTime() {
		return waitTime;
	}

	public static void main(String[] args) {
		init(null);
		System.out.println(getConfig().reportDirMonitor);
	}

}
